
export default class DisconnectEnable<K> {
    /**  删除事件回调函数 */
    private callBacksArray = new Array<Disconnecter<K>>()
    fire(key: K) {
        this.callBacksArray.forEach(c => {
            if (c.Connected) c.call(key)
        })
    }
    size() {
        return this.callBacksArray.size();
    }
    /** 绑定删除事件回调函数 */
    bind(call: (key: K) => void) {
        const disconnecter: Disconnecter<K> = {
            Connected: true,
            Disconnect: () => { },
            call
        }
        this.callBacksArray.push(disconnecter)
        disconnecter.Disconnect = () => {
            if (disconnecter.Connected) {
                disconnecter.Connected = false;
                const index = this.callBacksArray.findIndex(c => c === disconnecter);
                this.callBacksArray.remove(index);
            }
        }
        return disconnecter
    }
}